package com.project.poetry.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.poetry.common.util.CommonUtil;
import com.project.poetry.service.entity.dto.forum.ForumListDTO;
import com.project.poetry.service.entity.dto.forum.ReleaseForumDTO;
import com.project.poetry.service.entity.po.common.CommentPO;
import com.project.poetry.service.entity.po.forum.ForumPO;
import com.project.poetry.service.entity.po.user.UserPO;
import com.project.poetry.service.mapper.ForumMapper;
import com.project.poetry.service.service.CommentService;
import com.project.poetry.service.service.ForumService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * 论坛业务层实现类
 * @author mengxj
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ForumServiceImpl extends ServiceImpl<ForumMapper, ForumPO> implements ForumService {

    @Resource
    private ForumMapper forumMapper;

    @Resource
    private CommentService commentService;

    @Override
    public void releaseForum(ReleaseForumDTO releaseForum, UserPO user) {
        ForumPO forum = new ForumPO();
        forum.setContent(releaseForum.getContent());
        forum.setUserId(user.getId());
        this.forumMapper.insert(forum);
    }

    @Override
    public List<ForumListDTO> forumList(Long userId) {
        List<ForumListDTO> list = this.forumMapper.forumList(userId);
        if(CommonUtil.isEmpty(list)){
            return Collections.emptyList();
        }
        list.forEach(dto -> dto.setCommons(this.commentService.getCommonsByForum(dto.getId())));
        return list;
    }

    @Override
    public void deleteById(Long forumId) {
        this.commentService.remove(new QueryWrapper<CommentPO>().lambda().eq(CommentPO :: getForumId, forumId));
        super.removeById(forumId);
    }
}
